/**
 * 2009/12/09
 */
package com.googlecode.derby.customize.sample.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.googlecode.derby.customize.DataControllable;
import com.googlecode.derby.customize.DerbyDataController;
import com.googlecode.derby.customize.ResultData;
import com.googlecode.derby.customize.sample.entity.SampleEntity;
import com.googlecode.derby.customize.sample.entity.SampleNonPkEntity;

/**
 * @author osamu tanaka
 */
public final class SampleJoinDao {

    /**
     * @param aDbName
     *            database name
     * @param user
     *            user
     * @param password
     *            password
     * @param key
     *            key
     * @return this
     */
    public static SampleJoinDao getInstance(
            final String aDbName,
            final String user,
            final String password,
            final String key) {
        final SampleJoinDao dao =
            new SampleJoinDao(
                aDbName,
                user,
                password,
                key);
        return dao;
    }

    private final String dbName;
    private final DataControllable dc;

    private SampleJoinDao(
            final String aDbName,
            final String user,
            final String password,
            final String key) {
        this.dc = DerbyDataController.getInstance(
                aDbName,
                user,
                password,
                key);
        this.dbName = aDbName;
    }

    /**
     * @return dbName database name
     */
    public String getDbName() {
        return this.dbName;
    }

    /**
     * @param clause
     *            条件式
     * @return List<ResultData>
     */
    public List<ResultData> select(final String clause) {
        if (StringUtils.isNotEmpty(this.dbName)) {
            if (StringUtils.isNotEmpty(clause)) {
                return this.dc
                        .selectTables(
                                SampleNonPkEntity.class,
                                SampleEntity.class
                                )
                        .where("SAMPLE.USER_ID = SAMPLE_NONPK.USER_ID")
                        .where(clause)
                        .getResultDatas();
            }
            return this.dc
                    .selectTables(
                            SampleNonPkEntity.class,
                            SampleEntity.class
                    )
                    .where("SAMPLE.USER_ID = SAMPLE_NONPK.USER_ID")
                    .getResultDatas();
        }
        return null;
    }

}
